package util;

import org.scribe.model.Token;

import com.google.code.linkedinapi.client.LinkedInApiClient;
import com.google.code.linkedinapi.client.LinkedInApiClientFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInAccessToken;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;

import domain.User;

public class ApiClientBuilder {
	private LinkedInApiClient linkedInApiClient;
	private FacebookClient facebookClient;
	
	public LinkedInApiClient getLinkedInApiClient() {
		return linkedInApiClient;
	}
	
	public FacebookClient getFacebookClient() {
		return facebookClient;
	}
	
	public boolean buildLinkedInApiClient(User user,
			final String LINKEDIN_API_KEY, final String LINKEDIN_API_SECRET) {
		
		if (user != null && user.getSocialToken() != null && user.getSocialToken().getLinkedInToken() != null) {
			LinkedInAccessToken accessToken = new LinkedInAccessToken(user.getSocialToken().getLinkedInToken(), user.getSocialToken().getLinkedinTokenSecret());
			LinkedInApiClientFactory factory = LinkedInApiClientFactory.newInstance(LINKEDIN_API_KEY, LINKEDIN_API_SECRET);
			this.linkedInApiClient = factory.createLinkedInApiClient(accessToken);
			return true;
		}
		
		return false;
	}
	
	public boolean buildFacebookClient(User user, final String FACEBOOK_API_KEY, final String FACEBOOK_API_SECRET) {
		if (user != null && user.getSocialToken() != null && user.getSocialToken().getFacebooktoken() != null) {
			Token accessToken = new Token(user.getSocialToken().getFacebooktoken(), user.getSocialToken().getFacebooktokensecret());
			this.facebookClient = new DefaultFacebookClient(accessToken.getToken());
			return true;
		}
		
		return false;
	}
}
